package com.yanqu.road.dao.impl.activity.carptrip;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.carptrip.data.CarpTripDrawNoticeData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CarpTripDrawNoticeDataDaoImpl extends TempDataObjectDao<CarpTripDrawNoticeData> {


    public static String getCarpTripDrawNoticeDataSql = "select * from `t_c_carp_trip_draw_notice` where `activityId`=? order by getTime desc limit ?";
    public List<CarpTripDrawNoticeData> getCarpTripDrawNoticeData(int activityId, int count) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<CarpTripDrawNoticeData> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCarpTripDrawNoticeDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, count);
            rs = call.executeQuery();
            while (rs.next()) {
                CarpTripDrawNoticeData data = parse(rs);
                list.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }



    public static String updateCarpTripDrawNoticeDataSql = "update `t_c_carp_trip_draw_notice` set userId=?,serverId=?,nickName=?,reward=?,getTime=?,param=? where activityId=?";
    @Override
    protected boolean updateData(CarpTripDrawNoticeData carpTripDrawNoticeData) {
        carpTripDrawNoticeData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, carpTripDrawNoticeData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, carpTripDrawNoticeData.getServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, carpTripDrawNoticeData.getNickName()));
        para.put(i++, new DbParameter(Types.VARCHAR, carpTripDrawNoticeData.getReward()));
        para.put(i++, new DbParameter(Types.BIGINT, carpTripDrawNoticeData.getGetTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, carpTripDrawNoticeData.getParam()));
        para.put(i++, new DbParameter(Types.INTEGER, carpTripDrawNoticeData.getActivityId()));
        if (getDb().executeNoneQuery(updateCarpTripDrawNoticeDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCarpTripDrawNoticeDataSql = "insert into `t_c_carp_trip_draw_notice`(activityId,userId,serverId,nickName,reward,getTime,param) values (?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CarpTripDrawNoticeData carpTripDrawNoticeData) {
        carpTripDrawNoticeData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, carpTripDrawNoticeData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, carpTripDrawNoticeData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, carpTripDrawNoticeData.getServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, carpTripDrawNoticeData.getNickName()));
        para.put(i++, new DbParameter(Types.VARCHAR, carpTripDrawNoticeData.getReward()));
        para.put(i++, new DbParameter(Types.BIGINT, carpTripDrawNoticeData.getGetTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, carpTripDrawNoticeData.getParam()));
        if (getDb().executeNoneQuery(insertCarpTripDrawNoticeDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CarpTripDrawNoticeData parseData(ResultSet rs) throws SQLException {
        CarpTripDrawNoticeData carpTripDrawNoticeData = new CarpTripDrawNoticeData();
        carpTripDrawNoticeData.setActivityId(rs.getInt("activityId"));
        carpTripDrawNoticeData.setUserId(rs.getLong("userId"));
        carpTripDrawNoticeData.setServerId(rs.getLong("serverId"));
        carpTripDrawNoticeData.setNickName(rs.getString("nickName"));
        carpTripDrawNoticeData.setReward(rs.getString("reward"));
        carpTripDrawNoticeData.setGetTime(rs.getLong("getTime"));
        carpTripDrawNoticeData.setParam(rs.getString("param"));

        return carpTripDrawNoticeData;
    }
}
